package com.uhealin;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MR1 {

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		
		if(args.length != 4){
			System.out.println("args0:dst");
			System.out.println("args1:out");
			System.out.println("args2:splitMB");
			System.out.println("args3:reduce num");
			System.exit(0);
		}
        String In = args[0];
        String Out = args[1];
        int SplitMB = Integer.valueOf(args[2]);
 
        Configuration conf = new Configuration();
		conf.set("mapreduce.input.fileinputformat.split.maxsize", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapred.min.split.size", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapreduce.input.fileinputformat.split.minsize.per.node", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapreduce.input.fileinputformat.split.minsize.per.rack", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapred.job.reuse.jvm.num.tasks", "-1");
		conf.set("mapred.child.java.opts", "-Xmx350m -Xmx1024m");
        Job job = Job.getInstance(conf, "Hotel");
        
        job.setJarByClass(MR1.class);
        FileSystem fs = FileSystem.get(conf);
        fs.delete(new Path(Out), true);
 
        FileInputFormat.addInputPath(job, new Path(In));
        FileOutputFormat.setOutputPath(job, new Path(Out));
 
        job.setMapperClass(HotelMapper.class);
        job.setMapOutputKeyClass(Hotel.class);
        job.setMapOutputValueClass(NullWritable.class);
 
        job.setReducerClass(HotelReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);
 
        job.setNumReduceTasks(Integer.valueOf(args[3]));
        job.setGroupingComparatorClass(HotelGrouping.class);
        job.setPartitionerClass(HotelPartitioner.class);
        job.waitForCompletion(true);

	}
	
	
}
